/**
 * 
 */
package org.springside.examples.miniweb.web.report;

import java.util.List;

import javax.persistence.Transient;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springside.examples.miniweb.entity.search.SearchForm;

/**
 * @author Eric 2012-3-14
 * 
 * @since 6.0
 * 
 */
public class ReportController<T> {

	private final int ITEMSPERPAGE = 25;
	
	/**
	 * Get pages object for all search form and search pages.
	 * 
	 * @param searchFrom
	 * @param list
	 * @return
	 */
	@Transient
	protected Page<T> getPages(SearchForm searchFrom,
			List<T> list) {
		int pageNumber = Integer.parseInt(searchFrom.getPageNumber());
		int maxRecords = (pageNumber + 1) * ITEMSPERPAGE > list.size() ? list.size():(pageNumber + 1) * ITEMSPERPAGE;
		Page<T> pages = new PageImpl<T>(
				list.subList(pageNumber * ITEMSPERPAGE, maxRecords),
				new PageRequest(pageNumber, ITEMSPERPAGE, new Sort(Direction.ASC, "id")),
				list.size());
		return pages;
	}
}
